package org.mathig.poker;

/**
 * Card of poker (Rank + Color).
 * 
 * @author mathig
 */
public class Card implements Comparable<Card> {

	private Rank rank;

	private Color color;

	public Card(Rank rank, Color color) {
		this.rank = rank;
		this.color = color;
	}

	@Override
	public String toString() {
		return rank.getCode() + "" + color.getCode();
	}

	public Rank getRank() {
		return rank;
	}

	public void setRank(Rank rank) {
		this.rank = rank;
	}

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public int compareTo(Card c) {
		int compare = rank.compareTo(c.rank);
		if (compare != 0)
			return compare;
		return color.compareTo(c.color);
	}

	public static Card parse(String s) {
		Rank rank = null;
		for (Rank r : Rank.values())
			if (r.getCode().equals(String.valueOf(s.charAt(0))))
				rank = r;
		Color color = null;
		for (Color c : Color.values())
			if (c.getCode().equals(String.valueOf(s.charAt(1))))
				color = c;
		return new Card(rank, color);
	}
}
